15. 使用 Include 防范
ifndef
在本例中,你并不需要 ifndef 语句,因为代码很简单。你只写了一个类,所以不可能多次包含另一个类。但是,用 ifndef 语句来编写包含警卫是个好习惯。
在本练习中,你需要使用 ifndef 语法在 matrix.h 文件中编写一个 Include 防范程序。
Start Quiz:
#include <iostream>
#include <vector>
#include "matrix.h"
int main () {
// TODO: Nothing to do here
return 0;
}
#include "matrix.h"
Matrix::Matrix() {
std::vector <std:: vector <float> > initial_grid (10, std::vector <float>(5, 0.5));
grid = initial_grid;
rows = initial_grid.size();
cols = initial_grid[0].size();
}
Matrix::Matrix(std::vector <std:: vector <float> > initial_grid) {
grid = initial_grid;
rows = initial_grid.size();
cols = initial_grid[0].size();
}
void Matrix::setGrid(std::vector< std::vector<float> > new_grid) {
grid = new_grid;
rows = new_grid.size();
cols = new_grid[0].size();
}
std::vector< std::vector<float> > Matrix::getGrid() {
return grid;
}
std::vector<float>::size_type Matrix::getRows() {
return rows;
}
std::vector<float>::size_type Matrix::getCols() {
return cols;
}
Matrix Matrix::matrix_transpose() {
std::vector< std::vector<float> > new_grid;
std::vector<float> row;
for (int i = 0; i < cols; i++) {
row.clear();
for (int j = 0; j < rows; j++) {
row.push_back(grid[j][i]);
}
new_grid.push_back(row);
}
return Matrix(new_grid);
}
Matrix Matrix::matrix_addition(Matrix other) {
if ((rows != other.getRows()) || (cols != other.getCols())) {
throw std::invalid_argument( "matrices are not the same size" );
}
std::vector< std::vector<float> > othergrid = other.getGrid();
std::vector< std::vector<float> > result;
std::vector<float> new_row;
for (int i = 0; i < rows; i++) {
new_row.clear();
for (int j = 0; j < cols; j++) {
new_row.push_back(grid[i][j] + othergrid[i][j]);
}
result.push_back(new_row);
}
return Matrix(result);
}
void Matrix::matrix_print() {
std::cout << std::endl;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
std::cout << grid[i][j] << " ";
}
std::cout << std::endl;
}
std::cout << std::endl;
}
#include <vector>
#include <iostream>
#include <stdexcept>
class Matrix
{
private:
std::vector< std::vector<float> > grid;
std::vector<float>::size_type rows;
std::vector<float>::size_type cols;
public:
// constructor functions
Matrix ();
Matrix (std::vector< std::vector<float> >);
// set functions
void setGrid(std::vector< std::vector<float> >);
// get functions
std::vector< std::vector<float> > getGrid();
std::vector<float>::size_type getRows();
std::vector<float>::size_type getCols();
// matrix functions
Matrix matrix_transpose();
Matrix matrix_addition(Matrix);
// matrix printing
void matrix_print();
};
matrix.h 参考答案
# ifndef MATRIX_H
# define MATRIX_H
# include <vector>
# include <iostream>
# include <stdexcept>
# include <vector>
class Matrix
{
private:
std::vector< std::vector<float> > grid;
std::vector<int>::size_type rows;
std::vector<int>::size_type cols;
public:
//构造函数
Matrix ();
Matrix (std::vector< std::vector<float> >);
// set 函数
void setGrid(std::vector< std::vector<float> >);
// get 函数
std::vector< std::vector<float> > getGrid();
std::vector<int>::size_type getRows();
std::vector<int>::size_type getCols();
// matrix functions
Matrix matrix_tranpose();
Matrix matrix_addition(Matrix);
// matrix printing
void matrix_print();
};
# endif /* MATRIX_H */